package com.admin.service.serviceimpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.admin.mapper.UserMapper;
import com.admin.service.RoleUserService;
import com.admin.service.UserService;
import com.admin.util.cacheUtil.JedisUtil;
import com.fegin.bean.admin.AdminUser;
import com.fegin.bean.admin.RoleUser;
import com.fegin.bean.common.layuiBean.ResultPage;
import com.fegin.bean.common.layuiBean.VoPage;
import com.util.Respons.ResponseUtil;
import com.util.exception.CommonException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2019-01-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, AdminUser> implements UserService {
    @Autowired
    RoleUserService roleUserService;
    @Autowired
    JedisUtil jedisUtil;

    @Override
    public AdminUser login(String userName,String passWord) {
        return baseMapper.login(userName,passWord);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> delUser(Long userId, String userName ) {
        if(this.removeById(userId)){
            Map map=new HashMap();

            map.put("user_id",userId);
            roleUserService.removeByMap(map);
            //清楚 session共享缓存
            jedisUtil.delSessionCache(userName);
        }
        return  ResponseUtil.getSuccessMap();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertUser(AdminUser user,Long roleId,Long loginId) {
        user.setCreateTime(System.currentTimeMillis());
        AdminUser adminUser=this.getById(loginId);
        //如果管理员父级ID不是0   或者 父级管理员是0 代理商也是0  那么添加的用户 就是管理员
        if(adminUser.getAdminParentId()!=0||adminUser.getAdminParentId()==0&&adminUser.getAgentParentId()==0){
            user.setAdminParentId(loginId);
        }else{
            user.setAgentParentId(loginId);
        }
        if(this.save(user)){
            RoleUser roleUser=new RoleUser();
            roleUser.setRoleId(roleId);
            roleUser.setUserId(user.getId());
            if(roleUserService.save(roleUser)){
                return ResponseUtil.getSuccessMap();
            }
        }
        throw  new CommonException("新增失败");
    }

    @Override
    public ResultPage<AdminUser> findUserByPage(VoPage voPage,Long loginId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("loginId",loginId);
        List<AdminUser> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<AdminUser> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public ResultPage<AdminUser> findDistributionPeoPleByPage(VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<AdminUser> list = baseMapper.findDistributionPeoPleByPage(paramMap);
        Integer count = baseMapper.countDistributionPeoPleByPage(paramMap);
        ResultPage<AdminUser> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

}
